package com.zhouyu.service;

import org.springframework.beans.BeansException;
import org.springframework.beans.PropertyValues;
import org.springframework.beans.factory.config.InstantiationAwareBeanPostProcessor;
import org.springframework.stereotype.Component;

import java.lang.reflect.InvocationTargetException;

/**
 * @author leimingchao
 * create 2025-05-03 17:52
 */

//@Component
public class ZhouyuInstantiationAwareBeanPostProcessor implements InstantiationAwareBeanPostProcessor {


	@Override
	public Object postProcessBeforeInstantiation(Class<?> beanClass, String beanName) throws BeansException {

		//if ("userService".equals(beanName)) {
		//
		//	Object instance = null;
		//	try {
		//		instance = beanClass.getConstructor().newInstance();
		//	} catch (InstantiationException e) {
		//		e.printStackTrace();
		//	} catch (IllegalAccessException e) {
		//		e.printStackTrace();
		//	} catch (InvocationTargetException e) {
		//		e.printStackTrace();
		//	} catch (NoSuchMethodException e) {
		//		e.printStackTrace();
		//	}
		//	return instance;
		//}

		return null;
	}

	@Override
	public boolean postProcessAfterInstantiation(Object bean, String beanName) throws BeansException {
		return InstantiationAwareBeanPostProcessor.super.postProcessAfterInstantiation(bean, beanName);
	}

	@Override
	public PropertyValues postProcessProperties(PropertyValues pvs, Object bean, String beanName) throws BeansException {
		//@Autowired，@Resource，@Value
		return InstantiationAwareBeanPostProcessor.super.postProcessProperties(pvs, bean, beanName);
	}

	@Override
	public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {

		if ("userService".equals(beanName)) {
			System.out.println("初始化前");
		}

		return bean;
	}

	@Override
	public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {

		if ("userService".equals(beanName)) {
			System.out.println("初始化后");
		}

		return bean;
	}


}
